﻿
using QFramework;
using UnityEngine;

namespace ProjectChildhood
{
    public class ToolSeed : ITool, IController
    {
        public string Name => "种子";

        public int Range => 1;

        public ToolbarSlot Slot { get; set; }


        public bool Selectable(ToolData toolData)
        {
            return toolData.ShowGrid[toolData.CellPos.x, toolData.CellPos.y] != null &&
                   !toolData.ShowGrid[toolData.CellPos.x, toolData.CellPos.y].HasPlant &&
                   Slot.Count > 0;
        }

        public void Use(ToolData toolData)
        {
            //种植Logic
            this.SendCommand(new SubItemCountCommand(Slot.ItemName, 1));

            Item itemConfig = Config.ItemWithName[Slot.ItemName];

            GameObject plantObj = ResController.Instance.LoadPrefab(itemConfig.PlantPrefab)
                .Instantiate()
                .Position(toolData.GridCenterPos); // 实例化一枚种子

            IPlant plant = plantObj.GetComponent<IPlant>();
            plant.XCell = toolData.CellPos.x;
            plant.YCell = toolData.CellPos.y;

            // 添加植物管理模块
            Object.FindObjectOfType<GridController>().PlantDatas[plant.XCell, plant.YCell] = plant;

            // 更新土地信息
            toolData.ShowGrid[plant.XCell, plant.YCell].HasPlant = true;
            toolData.ShowGrid[plant.XCell, plant.YCell].PlantName = plant.GetName();

            //音效
            AudioController.Instance.SFX_PlaceSeed.Play();
            CameraController.Shake(CameraController.EShakeType.Middle);
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }

    }
}
